<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../ct-failure-card-buttons.html">
<link rel="import" href="../ct-step-failure-card.html">
<link rel="import" href="../../lib/analytics-mock.html">

<link rel="import" href="../../model/ct-commit-list-mock.html">
<link rel="import" href="../../model/ct-commit-log-mock.html">
<link rel="import" href="../../model/ct-failure-group.html">

<script>
(function () {

var assert = chai.assert;

describe('ct-failure-card-buttons', function() {
  var group;
  var card;
  var failures;

  beforeEach(function(done) {
    card = document.createElement('ct-failure-card-buttons');
    var cl = new CTCommitListMock();
    group = new CTFailureGroup('blink', new CTStepFailureGroupData([
        new CTStepFailure('autobot', 'unknown', {someBuilder: {key: 'a'}}, {'blink':158547},
                          {'blink':158544})], cl));
    card.group = group;
    setTimeout(done);
  });

  describe('failure card buttons', function() {
    it('examine should link to failure', function() {
      assert(card.shadowRoot.getElementById('examineLink').href.endsWith(
          '/blink/failure/autobot%3A%3Aunknown'));
    });

    it('clicking link bug should show dialog', function(done) {
      card.shadowRoot.getElementById('link-bug').dispatchEvent(new CustomEvent('tap'));
      setTimeout(function() {
        var dialog = document.querySelector('::shadow #bugDialog');
        assert.isTrue(dialog.opened);
        var bugField = document.querySelector('::shadow #bug');
        bugField.value = '999';
        document.querySelector('::shadow #dialogOk').dispatchEvent(new CustomEvent('tap'));
        setTimeout(function() {
          assert.equal(group.bug, 'https://crbug.com/999');
          assert.equal(group.bugLabel, 'Bug 999');
          assert.equal(group._annotation.bug, 'https://crbug.com/999');
          assert(!dialog.opened, 'dialog should be closed after clicking ok');
          done();
        });
      });
    });

    it('entering URLs should work for bugs', function(done) {
      card.shadowRoot.getElementById('link-bug').dispatchEvent(new CustomEvent('tap'));
      setTimeout(function() {
        var dialog = document.querySelector('::shadow #bugDialog');
        assert.isTrue(dialog.opened);
        var bugField = document.querySelector('::shadow #bug');
        bugField.value = 'https://foo.com/?id=888';
        document.querySelector('::shadow #dialogOk').dispatchEvent(new CustomEvent('tap'));
        setTimeout(function() {
          assert.equal(group.bug, 'https://foo.com/?id=888');
          assert.equal(group.bugLabel, 'Bug 888');
          assert.equal(group._annotation.bug, 'https://foo.com/?id=888');
          assert(!dialog.opened, 'dialog should be closed');
          done();
        });
      });
    });

    it('file bug link should have the right URL', function(done) {
      card.shadowRoot.getElementById('link-bug').dispatchEvent(new CustomEvent('tap'));
      setTimeout(function() {
        var dialog = document.querySelector('::shadow #bugDialog');
        assert.isTrue(dialog.opened);
        var fileBugLink = document.querySelector('::shadow #fileBugLink');
        assert.equal(fileBugLink.href, 'https://code.google.com/p/chromium/issues/entry?status=Available&labels=gardening-blink,Pri-2&summary=Build%20failure&comment=Build%20is%20broken%3A%0Aautobot%20unknown%0Ahttp%3A%2F%2Ftest-results.appspot.com%2Fdashboards%2Fflakiness_dashboard.html%23tests%3Dunknown%26testType%3Dautobot%0A%0ARevision%20range%3A%0Ablink%20158544%20%3A%20158545%0A%0AFailing%20builders%3A%0AsomeBuilder%3A%20undefined%2Fbuilders%2FsomeBuilder%0A');
        dialog.close();
        assert(!dialog.opened, 'dialog should be closed');
        done();
      });
    });

    it('remove bug link should work', function(done) {
      group.setBug(123);
      card.shadowRoot.getElementById('link-bug').dispatchEvent(new CustomEvent('tap'));
      setTimeout(function() {
        var dialog = document.querySelector('::shadow #bugDialog');
        assert.isTrue(dialog.opened);
        document.querySelector('::shadow #dialogRemoveBug').dispatchEvent(new CustomEvent('tap'));
        setTimeout(function() {
          assert.isUndefined(group.bug);
          assert.isUndefined(group.bugLabel);
          assert(!dialog.opened, 'dialog should be closed');
          done();
        });
      });
    });
  });

});

})()
</script>
